/**
 * 获取路由更改时的面包屑变化和导航变化
 *
 *
 */

import bus from "@/bus/mitt.js";
import {routerTo} from "@/utils/index.js";
import {useGetCompanyId} from "@/composition/billSys/useAccount.js";


// -----------------------------------------------------------1---------------------------------------------------------------------------------------

// 当前操作公司的名称路由
const currentCompanyName = ref("");
const currentCompanyId = ref(-1);

// 更新当前操作公司的名称路由
export const updateCurrentCompanyName = (name) => {
  currentCompanyName.value = name;
  currentCompanyId.value = useGetCompanyId(name);
  // 保存到localstorage
  localStorage.setItem("currentCompanyName", currentCompanyName.value);
  localStorage.setItem("currentCompanyId", currentCompanyId.value);
};

// 获取当前操作公司的名称路由Ref
export const getCurrentCompanyNameRef = () => {
  return currentCompanyName;
};

// 获取当前操作公司的名称路由
export const getCurrentCompanyName = () => {
  return currentCompanyName.value;
};

// 删除当前操作公司的名称路由
export const removeCurrentCompanyName = (to) => {
  if (to.meta.type !== 'company') {
    updateCurrentCompanyName('');
  }
};

// 初始化当前操作公司的名称路由 刷新不丢失
export const initCurrentCompanyName = () => {
  currentCompanyName.value = localStorage.getItem("currentCompanyName") || '';
  currentCompanyId.value = Number(localStorage.getItem("currentCompanyId") || '-1');
};

initCurrentCompanyName();


// -----------------------------------------------------------2---------------------------------------------------------------------------------------


const breadcrumbRef = ref([]);

// 获取当前面包屑代理对象
export const getBreadcrumbs = () => {
  return breadcrumbRef;
};

// 修改面包屑
export const setBreadcrumbs = (to) => {
  breadcrumbRef.value = to.meta.arr && to.meta.arr.map((name) => {
    return {name};
  });
  if (to.meta.type === 'company') {
    breadcrumbRef.value.unshift({name: getCurrentCompanyName()});
  }
  // 保存到localStorage
  localStorage.setItem('curBreadcrumb', JSON.stringify(breadcrumbRef.value));
};

// 增加数据初始化 刷新不丢失
export const initBreadcrumbs = () => {
  const curBreadcrumb = localStorage.getItem('curBreadcrumb');
  if (curBreadcrumb) {
    breadcrumbRef.value = JSON.parse(curBreadcrumb);
  }
};

initBreadcrumbs();

bus.$on("routeChangeAfter", setBreadcrumbs);


// -----------------------------------------------------------3---------------------------------------------------------------------------------------


// 当前路由的名称
const currentRouteName = ref("");

// 更新当前路由名称
export const updateCurrentRouteName = (to) => {
  currentRouteName.value = to.name;
  // 保存到localstorage
  localStorage.setItem("currentRouteName", currentRouteName.value);
};

// 获取当前路由名称
export const getCurrentRouteNameRef = () => {
  return currentRouteName;
};

// 获取当前路由名称
export const getCurrentRouteName = () => {
  return currentRouteName.value;
};

// 增加当前路由名称初始化 刷新不丢失
export const initCurrentRouteName = () => {
  updateCurrentRouteName({name: localStorage['currentRouteName'] || ''});
};
//
initCurrentRouteName();
bus.$on("routeChangeAfter", updateCurrentRouteName);



// -----------------------------------------------------------4---------------------------------------------------------------------------------------



const routeHistoryList = ref([]);

// 获取当前路由历史代理对象
export const getRouteHistoryList = () => {
  return routeHistoryList;
};

// 添加路由记录列表
export const addRouteHistoryList = (to) => {
  const index = routeHistoryList.value.findIndex((item) => item.name === to.name && item.type === to.meta.type && item.companyName === getCurrentCompanyName());
  if (index !== -1) {
    // 路由跳转找到了就将这个设置为当前路由
    setCurrentRouteData(routeHistoryList.value[index]);
    return;
  }
  let companyName = '';
  let title = to.meta.title;
  if (to.meta.type === 'company') {
    companyName = getCurrentCompanyName();
    title = getCurrentCompanyName() +  '/' + title;
  }
  let dataTemp = {
    name: to.name,
    title,
    companyName,
    type: to.meta.type,
    reloadIndex: 0
  };
  routeHistoryList.value.push(dataTemp);
  setCurrentRouteData(dataTemp);
  // 保存数据到localstorage 保持数据
  localStorage.setItem("routeHistoryList", JSON.stringify(routeHistoryList.value));
};

// 删除路由记录列表
export const removeRouteHistoryList = (historyRoute) => {
  const index = routeHistoryList.value.findIndex((item) => item.name === historyRoute.name && item.type === historyRoute.type && item.companyName === historyRoute.companyName);
  if (index !== -1) {
    // 判断删除的是不是当前的路由
    routeHistoryList.value[index].reloadIndex++;
    let isCurrentRoute = historyRoute.name === getCurrentRouteName() && historyRoute.companyName === getCurrentCompanyName();
    routeHistoryList.value.splice(index, 1);
    localStorage.setItem("routeHistoryList", JSON.stringify(routeHistoryList.value));
    if (isCurrentRoute) {
      let newIndex = (index - 1) > 0 ? (index - 1) : 0;
      updateCurrentCompanyName(routeHistoryList.value[newIndex].companyName);
      // 跳转到上一个路由
      routerTo(routeHistoryList.value[newIndex].name, 11);
    }
  }
};

// 增加数据初始化 刷新不丢失
export const initRouteHistoryList = () => {
  routeHistoryList.value = JSON.parse(localStorage.getItem("routeHistoryList")) || [];
};
initRouteHistoryList();
bus.$on("routeChangeBefore", addRouteHistoryList);


// -----------------------------------------------------------5---------------------------------------------------------------------------------------

const curRouteData = ref({});

// 获取当前路由信息
export const getCurrentRouteData = () => {
  return curRouteData.value;
};

// 获取当前路由信息Ref
export const getCurrentRouteDataRef = () => {
  return curRouteData;
};

// 设置当前路由信息
export const setCurrentRouteData = (data) => {
  curRouteData.value = data;
};

// 修改当前路由信息的刷新次数
export const updateCurrentRouteDataReloadIndex = () => {
  curRouteData.value.reloadIndex++;
  bus.$emit('refreshPage', null);
};




